package com.qumitech.visualnovel.service.app.impl;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.dao.console.UserInfo;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.app.ThirdBindCfgAdminDTO;
import com.qumitech.visualnovel.pubdef.dto.app.ThirdBindCfgAdminQueryDTO;
import com.qumitech.visualnovel.pubdef.dto.app.ThirdBindCfgDetailAdminDTO;
import com.qumitech.visualnovel.pubdef.dto.app.ThirdBindCfgDetailAdminQueryDTO;
import com.qumitech.visualnovel.pubdef.dto.group.GroupDetailAdminDTO;
import com.qumitech.visualnovel.service.app.AppThirdBindService;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author :  ZhChNing
 * @Descriptiom :
 * @Date Created in 2019/11/19 14:27
 */
@Service
public class AppThirdBindServiceImpl implements AppThirdBindService {

    private final String detailListUrl = "/thirdbind/console/detail/list";
    private final String detailBaseListUrl = "/thirdbind/console/detail/base/list";

    private final String addDetailUrl = "/thirdbind/console/detail";
    private final String updateDetailtUrl = "/thirdbind/console/detail/";

    private final String cfgListUrl = "/thirdbind/console/cfg/list";
    private final String addCfgUrl = "/thirdbind/console/cfg";
    private final String updateCfgUrl = "/thirdbind/console/cfg/";

    @Override
    public Map<String, Object> listThirdBindCfgDetailDTO(ThirdBindCfgDetailAdminQueryDTO queryDTO) {
        String url = UrlConstant.SERVER_URL + detailListUrl;
        Map<String, String> paramMap = new HashMap<>();

        if (queryDTO.getOffset() != null) {
            paramMap.put("offset", queryDTO.getOffset().toString());
        }
        if (queryDTO.getLimit() != null) {
            paramMap.put("limit", queryDTO.getLimit().toString());
        }
        if (queryDTO.getStartTime() != null) {
            paramMap.put("startTime", queryDTO.getStartTime().toString());
        }
        if (queryDTO.getEndTime() != null) {
            paramMap.put("endTime", queryDTO.getEndTime().toString());
        }
        if (queryDTO.getDeleteFlag() != null) {
            paramMap.put("delete_flag", queryDTO.getDeleteFlag().toString());
        }
        if (queryDTO.getCfgId() != null) {
            paramMap.put("cfg_id", queryDTO.getCfgId().toString());
        }
        if (queryDTO.getAppName() != null) {
            paramMap.put("app_name", queryDTO.getAppName().toString());
        }
        if (queryDTO.getBindType() != null) {
            paramMap.put("bind_type", queryDTO.getBindType().toString());
        }
        if (queryDTO.getThirdType() != null) {
            paramMap.put("third_type", queryDTO.getThirdType().toString());
        }
        if (queryDTO.getThirdAppId() != null) {
            paramMap.put("third_app_id", queryDTO.getThirdAppId().toString());
        }

        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        checkBaseRsp(rsp);

        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }

    @Override
    public Map<String, Object> listThirdBindCfgDetailBaseDTO(ThirdBindCfgDetailAdminQueryDTO queryDTO) {
        String url = UrlConstant.SERVER_URL + detailBaseListUrl;
        Map<String, String> paramMap = new HashMap<>();

        if (queryDTO.getOffset() != null) {
            paramMap.put("offset", queryDTO.getOffset().toString());
        }
        if (queryDTO.getLimit() != null) {
            paramMap.put("limit", queryDTO.getLimit().toString());
        }
        if (queryDTO.getStartTime() != null) {
            paramMap.put("startTime", queryDTO.getStartTime().toString());
        }
        if (queryDTO.getEndTime() != null) {
            paramMap.put("endTime", queryDTO.getEndTime().toString());
        }
        if (queryDTO.getDeleteFlag() != null) {
            paramMap.put("delete_flag", queryDTO.getDeleteFlag().toString());
        }
        if (queryDTO.getCfgId() != null) {
            paramMap.put("cfg_id", queryDTO.getCfgId().toString());
        }
        if (queryDTO.getAppName() != null) {
            paramMap.put("app_name", queryDTO.getAppName().toString());
        }

        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        checkBaseRsp(rsp);

        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }

    @Override
    public void addThirdBindCfgDetailAdminDTO(ThirdBindCfgDetailAdminDTO detailAdminDTO) {
        String url = UrlConstant.SERVER_URL + addDetailUrl;
        detailAdminDTO.setOperator(getOperatorName());
        String req = JSONObject.toJSONString(detailAdminDTO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        checkBaseRsp(rsp);
    }

    @Override
    public void updateThirdBindCfgDetailAdminDTO(ThirdBindCfgDetailAdminDTO detailAdminDTO) {
        String url = UrlConstant.SERVER_URL + updateDetailtUrl+detailAdminDTO.getCfgId();
        detailAdminDTO.setOperator(getOperatorName());
        String req = JSONObject.toJSONString(detailAdminDTO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        checkBaseRsp(rsp);
    }

    @Override
    public Map<String, Object> listThirdBindCfgDTO(ThirdBindCfgAdminQueryDTO queryDTO) {
        String url = UrlConstant.SERVER_URL + cfgListUrl;
        Map<String, String> paramMap = new HashMap<>();

        if (queryDTO.getOffset() != null) {
            paramMap.put("offset", queryDTO.getOffset().toString());
        }
        if (queryDTO.getLimit() != null) {
            paramMap.put("limit", queryDTO.getLimit().toString());
        }
        if (queryDTO.getStartTime() != null) {
            paramMap.put("startTime", queryDTO.getStartTime().toString());
        }
        if (queryDTO.getEndTime() != null) {
            paramMap.put("endTime", queryDTO.getEndTime().toString());
        }
        if (queryDTO.getDeleteFlag() != null) {
            paramMap.put("delete_flag", queryDTO.getDeleteFlag().toString());
        }
        if (queryDTO.getCfgId() != null) {
            paramMap.put("cfg_id", queryDTO.getCfgId().toString());
        }
        if (queryDTO.getBindId() != null) {
            paramMap.put("bind_id", queryDTO.getBindId().toString());
        }
        if (queryDTO.getBindType() != null) {
            paramMap.put("bind_type", queryDTO.getBindType().toString());
        }
        if (queryDTO.getThirdType() != null) {
            paramMap.put("third_type", queryDTO.getThirdType().toString());
        }
        if (queryDTO.getDeviceType() != null) {
            paramMap.put("device_type", queryDTO.getDeviceType().toString());
        }
        if (queryDTO.getAppPackageName() != null) {
            paramMap.put("app_package_name", queryDTO.getAppPackageName().toString());
        }
        if (queryDTO.getBindAppPackageFlag() != null) {
            paramMap.put("bind_app_package_flag", queryDTO.getBindAppPackageFlag().toString());
        }

        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        checkBaseRsp(rsp);

        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }

    @Override
    public void addThirdBindCfgAdminDTO(ThirdBindCfgAdminDTO detailAdminDTO) {
        String url = UrlConstant.SERVER_URL + addCfgUrl;
        detailAdminDTO.setOperator(getOperatorName());
        String req = JSONObject.toJSONString(detailAdminDTO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        checkBaseRsp(rsp);
    }

    @Override
    public void updateThirdBindCfgAdminDTO(ThirdBindCfgAdminDTO detailAdminDTO) {
        String url = UrlConstant.SERVER_URL + updateCfgUrl+detailAdminDTO.getBindId();
        detailAdminDTO.setOperator(getOperatorName());
        String req = JSONObject.toJSONString(detailAdminDTO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        checkBaseRsp(rsp);
    }

    private void checkBaseRsp( BaseRsp rsp){
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            if (rsp == null) {
                throw new BusinessException(ResultEnum.FAILURE);
            }
            Map<String, Object> rspMap = (Map<String, Object>) rsp.getError();
            ResultEnum resultEnum = ResultEnum.FAILURE;
            resultEnum.setMsg(rspMap.get("description").toString());
            resultEnum.setState(Integer.valueOf(rspMap.get("status").toString()));
            throw new BusinessException(resultEnum);
        }
    }

    private String getOperatorName() {
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        return activeUser.getUsername();
    }
}
